package com.noname.lib_base_java.net.download;

import com.noname.lib_base_java.util.L;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Author: Lmoumou
 * Date: 2019-01-22 11:29
 * 文件名: DownloadInterceptor
 * 描述:
 */
public class DownloadInterceptor implements Interceptor {
    private DownloadProgressListener listener;

    public DownloadInterceptor(DownloadProgressListener listener) {
        this.listener = listener;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());
        ResponseBody body = originalResponse.body();
        MediaType mediaType = body.contentType();
        long l = body.contentLength();
        L.e("DownloadInterceptor","contentLength->"+l);
        L.e("DownloadInterceptor","mediaType->"+mediaType.toString());
        return originalResponse.newBuilder()
                .body(new DownloadResponseBody(originalResponse.body(), listener))
                .build();
    }
}
